﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Extentions;

namespace TrainingReminderSystem.Model
{
    /// <summary>
    /// 客户信息
    /// </summary>
    public partial class SysCustomer
    {
        #region 登录相关辅助属性和方法

        /// <summary>
        /// 非数据库字段
        /// </summary>
        [NotMapped]
        public string Token { get; set; }

        /// <summary>
        /// 验证登录用户的有效性
        /// </summary>
        /// <returns></returns>
        public string ValidateLogin()
        {
            if (this.EnumStatus != EnumStatus.启用)
            {
                return "operatorError";
            }
            if (this.SysStore == null || this.SysStore.EnumStatus != EnumStatus.启用)
            {
                return "sysEmployeeStoreError";
            }
            return null;
        }

        /// <summary>
        /// 验证登录用户的有效性的列表筛选where条件
        /// </summary>
        /// <returns></returns>
        public static Func<SysCustomer, bool> GetValidateLoginWhere()
        {
            Func<SysCustomer, bool> where = m =>
                m.EnumStatus == EnumStatus.启用 && m.SysStore != null && m.SysStore.EnumStatus == EnumStatus.启用;
            return where;
        }

        /// <summary>
        /// 密码的验证
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static Func<SysCustomer, bool> GetValidateLoginPwdWhere(string pwd)
        {
            Func<SysCustomer, bool> where = m => m.Pwd == pwd;
            return where;
        }

        /// <summary>
        /// 得到相关店铺
        /// </summary>
        /// <returns></returns>
        public SysStore GetCurrentStore()
        {
            if (this.SysStore == null || this.SysStore.EnumStatus != EnumStatus.启用)
            {
                return null;
            }

            var sysStore = this.SysStore;
            return sysStore;
        }

        public int GetParentID()
        {
            var sysStore = GetCurrentStore();
            if (sysStore == null) return -1;
            return sysStore.GetParentID();
        }

        #endregion
    }
}
